# -*- coding: utf-8 -*-
# (c) 2015 Mikel Arregi - AvanzOSC
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from collections import namedtuple
import json
import time

from odoo import api, fields, models, _
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
from odoo.tools.float_utils import float_compare
from odoo.addons.procurement.models import procurement
from odoo.exceptions import UserError


class stock_quant_mulit_pick(models.Model):
    _name = 'stock.quant.mulit.pick'

    picking_id = fields.Many2one('stock.picking', u'调拨单')
    location_id = fields.Many2one('stock.location', u'源库位')
    stock_quant_id = fields.Many2one('stock.quant', u'库存份',
                                     domain="[('location_id', '=', location_id),('qty', '>', 0),('reservation_id', '=', False)]")
    product_id = fields.Many2one('product.product', u'产品', related='stock_quant_id.product_id')
    product_uom_id = fields.Many2one('product.uom', u'单位', related='stock_quant_id.product_uom_id')
    lot_id = fields.Many2one('stock.production.lot', u'批次/序列号', related='stock_quant_id.lot_id')
    quant_qty = fields.Float(u'份数量', related='stock_quant_id.qty')
    pick_qty = fields.Float(u'挑选数', default=0.0)

    @api.onchange('stock_quant_id')
    def quant_onchange(self):
        if self.stock_quant_id:
            self.pick_qty = self.stock_quant_id.qty


            # name = "lot_id"
            # groups = "stock.group_production_lot" / >
            # name = "package_id"
            # groups = "stock.group_tracking_lot" / >


class stock_picking(models.Model):
    _inherit = 'stock.picking'

    stock_quant_mulit_picks = fields.One2many('stock.quant.mulit.pick', 'picking_id', u"批量选择库存")
    stock_quant_ids = fields.Many2many('stock.quant', string=u'库存份',
                                       domain="[('qty', '>', 0),('reservation_id', '=', False)]")

    @api.multi
    def get_pick_quant(self):
        pick_obj = self.env['stock.quant.mulit.pick']
        quants = self.stock_quant_ids
        # quants = self.env['stock.quant'].search(
        #     [('location_id', '=', self.location_id.id), ('qty', '>', 0), ('reservation_id', '=', False)])
        for quant in quants:
            pick_obj.create({
                'picking_id': self.id,
                'location_id': self.location_id.id,
                'stock_quant_id': quant.id,
            })

    @api.multi
    def quants_create_move(self):
        move_obj = self.env['stock.move']
        for picking in self:
            for quant_pick in picking.stock_quant_mulit_picks:
                if quant_pick.pick_qty <= 0:
                    continue
                quant = quant_pick.stock_quant_id
                # if 这里要考虑quant创建的要合并。不然就会出现负数调拨
                move = move_obj.create({
                    'name': quant.product_id.display_name,
                    'picking_id': self.id,
                    'date': self.create_date,
                    'product_id': quant.product_id.id,
                    'product_uom_qty': quant_pick.pick_qty,
                    'product_uom': quant.product_uom_id.id,
                    'procure_method': 'make_to_stock',
                    'location_id': picking.location_id.id,
                    'location_dest_id': picking.location_dest_id.id,
                })
                if move:
                    if move.picking_id:  # pragma: no cover
                        move.picking_id.recompute_pack_op = True
                    # for quant_id in move.reserved_quant_ids.ids:
                    #     move.write({'reserved_quant_ids': [[3, quant_id]]})
                    quants = []
                    quants.append([quant, quant.qty])
                    self.env['stock.quant'].quants_reserve(quants, move)
            picking.action_confirm()
            picking.do_prepare_partial()
            picking.action_assign()
